package com.cmict.data.system.controller.admin.conf;

import com.cmict.data.framework.common.pojo.CommonResult;
import com.cmict.data.framework.common.pojo.PageResult;
import com.cmict.data.framework.excel.core.util.ExcelUtils;
import com.cmict.data.framework.operatelog.core.annotations.OperateLog;
import com.cmict.data.system.controller.admin.conf.vo.*;
import com.cmict.data.system.convert.conf.ConfConvert;
import com.cmict.data.system.dal.dataobject.conf.ConfDO;
import com.cmict.data.system.service.conf.ConfService;
import com.cmict.data.system.controller.admin.conf.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static com.cmict.data.framework.common.pojo.CommonResult.success;
import static com.cmict.data.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;


/**
 * @author liutao
 * @date 2022/4/22
 */
@Api(tags = "管理后台 - 系统通用配置信息")
@RestController
@RequestMapping("/base/conf")
@Validated
public class ConfController {

    @Resource
    private ConfService confService;

    @PostMapping("/create")
    @ApiOperation("创建系统通用配置信息")
    @PreAuthorize("@ss.hasPermission('base:conf:create')")
    public CommonResult<Long> createConf(@Valid @RequestBody ConfCreateReqVO createReqVO) {
        return success(confService.createConf(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新系统通用配置信息")
    @PreAuthorize("@ss.hasPermission('base:conf:update')")
    public CommonResult<Boolean> updateConf(@Valid @RequestBody ConfUpdateReqVO updateReqVO) {
        confService.updateConf(updateReqVO);
        return success(true);
    }

    @GetMapping("/getConfListByconfKey")
    @ApiOperation("获取配置列表")
    @PreAuthorize("@ss.hasPermission('base:conf:query')")
    @ApiImplicitParam(name = "confKey", value = "编号",  dataTypeClass = String.class)
    public CommonResult<List<ConfRespVO>> getConfListByconfKey( String confKey) {
        List<ConfDO> list = confService.getConfListByconfKey(confKey);
        return success(ConfConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/getLoginConf")
    @ApiOperation("登录获取配置")
    @PermitAll
    @ApiImplicitParam(name = "confKey", value = "编号", required = true, dataTypeClass = String.class)
    public CommonResult<ConfRespVO> getLoginConf(String confKey) {
        ConfDO confDO = confService.getLoginConf(confKey);
        return success(ConfConvert.INSTANCE.convert(confDO));
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除系统通用配置信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('base:conf:delete')")
    public CommonResult<Boolean> deleteConf(@RequestParam("id") Long id) {
        confService.deleteConf(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得系统通用配置信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('base:conf:query')")
    public CommonResult<ConfRespVO> getConf(@RequestParam("id") Long id) {
        ConfDO conf = confService.getConf(id);
        return success(ConfConvert.INSTANCE.convert(conf));
    }

    @GetMapping("/list")
    @ApiOperation("获得系统通用配置信息列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('base:conf:query')")
    public CommonResult<List<ConfRespVO>> getConfList(@RequestParam("ids") Collection<Long> ids) {
        List<ConfDO> list = confService.getConfList(ids);
        return success(ConfConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得系统通用配置信息分页")
    @PreAuthorize("@ss.hasPermission('base:conf:query')")
    public CommonResult<PageResult<ConfRespVO>> getConfPage(@Valid ConfPageReqVO pageVO) {
        PageResult<ConfDO> pageResult = confService.getConfPage(pageVO);
        return success(ConfConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出系统通用配置信息 Excel")
    @PreAuthorize("@ss.hasPermission('base:conf:export')")
    @OperateLog(type = EXPORT)
    public void exportConfExcel(@Valid ConfExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ConfDO> list = confService.getConfList(exportReqVO);
        // 导出 Excel
        List<ConfExcelVO> datas = ConfConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "系统通用配置信息.xls", "数据", ConfExcelVO.class, datas);
    }

}
